/**
 * 外部返利维护 - 编辑页面
 * @author miaoyl
 * @date 2019/04/29
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: {
                GWDG003_NAME:null,
                GWDG005_NAME:null
            },
            rules: {
                GWDG001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWDG002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDG005_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWDG004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                crm_gwdc007_gwdg001:[]
            },
        },
        callObj,
        pageVue
    ;

    var Gwdc007Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwdc007_edit',
                _loadTplsFull: [
                    { dom: 'common_open_window', tpl: 'common/_open_window' }
                ]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwdc007/edit',
                xdata: vueData.form,
                loading: true,
                okmsg:self._moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        //app.pageModule('gwdc007').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwdc007/add',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwdc007').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            if (tagData.is_add) {
                vueData.isAdd=true;
                // app.log(obj);
                app.extendUpLow({
                    target: vueData.form,
                    res: {},
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }
            app.ajax({
                xurl: 'crm/gwdc007/getGwdelist',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.crm_gwdc007_gwdg001 = result.data.data;
                    }
                }
            });
        },
        _ready: function () {
            vueData.form = app.fields_(['GWDG_T'],vueData.form);
            pageVue = new Vue({
                el: '#gwdc007_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    openGwdg005:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwak001',
                                    columnList: [
                                        {label: '经销商编号', prop: 'GWAK001', width: '150'},
                                        {label: '经销商简称', prop: 'GWAKL003', width: '150'},
                                        {label: '经销商全称', prop: 'GWAKL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWDG005_NAME = data.GWAKL003;
                                    vueData.form.GWDG005 = data.GWAK001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择经销商',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGwdg003:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwah001',
                                    columnList: [
                                        {label: '产品分类编号', prop: 'GWAH001', width: '150'},
                                        {label: '说明', prop: 'GWAHL003', width: '150'},
                                        {label: '助记码', prop: 'GWAHL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWDG003_NAME = data.GWAHL003;
                                    vueData.form.GWDG003 = data.GWAH001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品分类',
                                area: ['600px', '465px']
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwdc007_edit');
                }
            });
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwdc007Edit();

});